// 创建一个简单的可读取流
const readableStream = new ReadableStream({
    start(controller) {
        const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
        let index = 0;
        const intervalId = setInterval(() => {
            if (index < data.length) {
                const chunk = data.slice(index, index + 3);
                controller.enqueue(chunk);
                index += 3;
            } else {
                controller.close();
                clearInterval(intervalId);
            }
        }, 1000);
    }
});
const reader = readableStream.getReader();

async function readFixedBytes(reader, byteCount) {
    const buffer = new Uint8Array(byteCount);
    let offset = 0;

    try {
        while (offset < byteCount) {
            const {value, done} = await reader.read();
            if (done) break;

            const chunk = value.subarray(0, Math.min(value.length, byteCount - offset));
            buffer.set(chunk, offset);
            offset += chunk.length;
        }
    } finally {
        reader.releaseLock();
    }

    return buffer.slice(0, offset);
}

readFixedBytes(reader, 5).then((result) => {
    console.log(result);
});